package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.intefaces.SubclassItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.Subclass;

/**
 * Created by lanouhn on 16/9/6.
 */
public class SubclassAdapter extends RecyclerView.Adapter<SubclassAdapter.ViewHolder> {
    private Context context;
    private Subclass subclass;
    private SubclassItemCliclListener subclassItemCliclListener;

    public void SubclassAdapter(SubclassItemCliclListener subclassItemCliclListener) {
        this.subclassItemCliclListener = subclassItemCliclListener;
    }

    public SubclassAdapter(Context context, Subclass subclass) {
        this.context = context;
        this.subclass = subclass;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_subclass_kindsdetails, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Subclass.DataBean.SubclassListBean bean = subclass.getData().getSubclass_list().get(position);
        Picasso.with(context).load(bean.getPic()).into(holder.iv_pic);
        holder.cb_subclass.setText(bean.getName());
//        holder.cb_subclass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                String id=subclass.getData().getSubclass_list().get(position).getId();
//                String ids=id;
//                if (isChecked) {
//                    Toast.makeText(context, ids+"被选中", Toast.LENGTH_SHORT).show();
//                } else {
//
//                    Toast.makeText(context, "取消选择", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
        if (subclassItemCliclListener != null) {

            holder.cb_subclass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String id = subclass.getData().getSubclass_list().get(position).getId();
                    subclassItemCliclListener.onItemClick(id, isChecked);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return subclass.getData().getSubclass_list().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_pic;
        private CheckBox cb_subclass;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_subclass);
            cb_subclass = (CheckBox) itemView.findViewById(R.id.cb_subclass);
        }
    }
}
